package com.ordos.recruit.module.batch.service;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ordos.recruit.module.batch.entity.RecruitBatchApplyDto;
import com.ordos.recruit.module.batch.entity.RecruitBatchApplyPo;
import com.ordos.recruit.module.batch.mapper.RecruitBatchApplyMapper;
import com.ordos.recruit.module.user.entity.SysSsoUserDto;
import com.ordos.recruit.module.user.util.UserUtil;
import com.wonders.commons.util.response.ApiResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * service
 *
 * @author jin
 */
@Service
public class RecruitBatchApplyService {

    private final RecruitBatchApplyMapper recruitBatchApplyMapper;

    public RecruitBatchApplyService(RecruitBatchApplyMapper recruitBatchApplyMapper) {
        this.recruitBatchApplyMapper = recruitBatchApplyMapper;
    }

    /**
     * 
     * 保存或更新申请表.
     *
     * @param recruitBatchApplyDto 请求入参.
     */
    public ApiResponse saveOrUpdate(RecruitBatchApplyDto recruitBatchApplyDto) {
        SysSsoUserDto sysSsoUserDto = UserUtil.getUserByRequest();
        RecruitBatchApplyPo recruitBatchApplyPo = new RecruitBatchApplyPo();
        if (StringUtils.isBlank(recruitBatchApplyDto.getId())) {
            BeanUtil.copyProperties(recruitBatchApplyDto, recruitBatchApplyPo);
            recruitBatchApplyPo.setId(UUID.randomUUID().toString().replace("-",""));
            recruitBatchApplyPo.setCreateUser(sysSsoUserDto.getUserid());
            recruitBatchApplyPo.setUpdateTime(new Date());
            recruitBatchApplyPo.setUpdateUser(sysSsoUserDto.getUserid());
            recruitBatchApplyMapper.insert(recruitBatchApplyPo);
        }else{
            BeanUtil.copyProperties(recruitBatchApplyDto, recruitBatchApplyPo);
            recruitBatchApplyPo.setUpdateTime(new Date());
            recruitBatchApplyPo.setUpdateUser(sysSsoUserDto.getUserid());
            recruitBatchApplyMapper.updateById(recruitBatchApplyPo);
        }
        return ApiResponse.success();
    }

    /**
     * 查询一条数据.
     * @param recruitBatchApplyDto 请求入参.
     * @return Po
     */
    public RecruitBatchApplyPo view(RecruitBatchApplyDto recruitBatchApplyDto) {
        return recruitBatchApplyMapper.selectById(recruitBatchApplyDto.getId());
    }

    /**
     * 查询列表数据.
     * @param recruitBatchApplyDto 请求入参.
     * @return Po
     */
    public List<RecruitBatchApplyPo> getList(RecruitBatchApplyDto recruitBatchApplyDto) {
        QueryWrapper<RecruitBatchApplyPo> queryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(recruitBatchApplyDto.getBatchName())){
            queryWrapper.like("BATCH_NAME", recruitBatchApplyDto.getBatchName());
        }
        queryWrapper.orderByDesc("UPDATE_TIME");
        return recruitBatchApplyMapper.selectList(queryWrapper);
    }
}
